export const httpRequest = (options: { data: any; header: {}; autoLoad: undefined; url: string; method: string, params }) => {
	return new Promise((resolve, reject) => {
    const api = 'https://knowledge.invn.com.cn/api'
		let header = {}
		let data = options.data
		
		if (options && options.header) header = options.header //传入header即用所传header
		if (wx.getStorageSync('token')) header.token = wx.getStorageSync('token')
		
		// 签名生成
		let url = api + options.url
		if (options.params) {
			const keys = Object.keys(options.params)
			keys.forEach((item, idx) => {
				if (idx == 0) {
					url += `?${item}=${options.params[item]}`
				} else {
					url += `&${item}=${options.params[item]}`
				}
			})
		}
		wx.request({
			url,
			method: options.method.toLocaleUpperCase(),
			data: data,
			header: header,
			success: (res) => {
				if (res.statusCode == 401) {
					wx.removeStorageSync('token')
					getApp().triggerListeners()

					wx.showModal({
						title: '提示',
						content: '您的登录状态已失效，是否重新登录？',
						showCancel: true,
						confirmText: '去登录',
						confirmColor: '#FFE501',
						success: ({ confirm, cancel }) => {
							if (confirm) {
								wx.redirectTo({ url: '/pages/login/login' })
							}
						}
					})
				}
				// 服务端错误
				if (res.statusCode !== 200) {
					let message = ''
					reject({
						code: 500,
						errorMsg: '服务端出错'
					})
				}
				if (res.data.success) {
					resolve(res.data)
				} else {
					reject(res.data)
				}
			},
			fail: (err) => {
				reject({
					code: 500,
					msg: '手机网络不太顺畅,请检查网络状况'
				})
			},
			complete: (err) => {
				
			}
		})
	})
}
